package com.dfqy.controller;

import com.dfqy.pojo.Appoint;
import com.dfqy.service.AppointService;
import com.dfqy.vo.Page;
import com.dfqy.vo.Result;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author : atyushu
 * Package : com.dfqy.controller
 * ClassName : AppointController
 * Create : 2023/8/1 - 13:19
 * Version : v1.0
 * Description : The class of controller for Appoint.
 */

@Validated
@RestController
@RequestMapping("appoint")
public class AppointController {

    @Autowired
    private AppointService appointService;

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        simpleDateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    @GetMapping("appointPage")
    public Page<Appoint> page(@RequestParam(name = "page", defaultValue = "1") @Range(min = 1, message = "页码过小") int page,
                              @RequestParam(name = "limit", defaultValue = "10") @Range(min = 1, message = "条数过小") int limit,
                              @RequestParam(name = "phone", required = false) String phone,
                              @RequestParam(name = "addrbranch", required = false) String addrbranch,
                              @RequestParam(name = "courseof", required = false) String courseof,
                              @RequestParam(name = "posttimeStart", required = false) Date posttimeStart,
                              @RequestParam(name = "posttimeEnd", required = false) Date posttimeEnd,
                              @RequestParam(name = "uname", required = false) String uname
    ) {
        return appointService.page(page, limit, phone, addrbranch, courseof, posttimeStart, posttimeEnd, uname);
    }

    @GetMapping("deleteAppoint/{id}")
    public Result delete(@PathVariable long id) {
        int delete = appointService.delete(id);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("batchDeleteAppoint/{id}")
    public Result batchDelete(@PathVariable String id) {
        String[] idStr = id.split(",");
        long[] ids = new long[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            ids[i] = Long.parseLong(idStr[i]);
        }
        int delete = appointService.delete(ids);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("updateAppoint")
    public Result update(@RequestBody Appoint appoint) {
        int update = appointService.update(appoint);
        return update > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("getAppointById/{id}")
    public Result getById(@PathVariable long id) {
        Appoint appoint = appointService.getById(id);
        return appoint != null ? Result.success(appoint) : Result.error("无数据");
    }
}
